﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HelperPrototype.SearchControls
{
    public partial class SearchPhieuDanhGia : UserControl
    {
        private DataGridView _dgvPhieuDG;
        private bool showAll = true;

        public DataGridView DgvPhieuDG
        {
            get { return _dgvPhieuDG; }
            set { _dgvPhieuDG = value; }
        }

        public SearchPhieuDanhGia()
        {
            InitializeComponent();
        }

        private void SearchPhieuDanhGia_Load(object sender, EventArgs e)
        {
            // event check change
            foreach (Control c in this.Controls)
                if (c.GetType() == typeof(CheckBox))
                {
                    ((CheckBox)c).CheckedChanged += this.CheckedChanged;
                }

            // load hanghoa
            _DB.DataClasses1DataContext dc = new _DB.DataClasses1DataContext();
            foreach (_DB.HangHoa h in dc.HangHoas)
            {
                clbHangHoa.Items.Add(h);
            }

            // load ncc
            foreach (_DB.NhaCungCap ncc in dc.NhaCungCaps)
            {
                clbNhaCungCap.Items.Add(ncc);
            }

            // load nguoi lap, nguoi duyet
            foreach (_DB.NguoiDung u in dc.NguoiDungs)
            {
                cbbNguoiTao.Items.Add(u.Ten);
                cbbNguoiDuyet.Items.Add(u.Ten);
            }

            // load trang thai
            foreach (_DB.TrangThaiPhieu t in dc.TrangThaiPhieus)
            {
                cbbTrangThai.Items.Add(t.MoTa);
            }

            //
            reloadChecked();
        }

        private void reloadChecked()
        {
            dtpNgayTaoMin.Enabled = cbNgayTaoMin.Checked;
            dtpNgayTaoMax.Enabled = cbNgayTaoMax.Checked;
            dtpNgayDuyetMin.Enabled = cbNgayDuyetMin.Checked;
            dtpNgayDuyetMax.Enabled = cbNgayDuyetMax.Checked;
            cbbNguoiTao.Enabled = cbNguoiTao.Checked;
            cbbNguoiDuyet.Enabled = cbNguoiDuyet.Checked;
            cbbTrangThai.Enabled = cbTrangThai.Checked;
            clbHangHoa.Enabled = cbHangHoa.Checked;
            clbNhaCungCap.Enabled = cbNhaCungCap.Checked;
        }

        private void CheckedChanged(object sender, EventArgs e)
        {
            reloadChecked();
        }

        IQueryable<_DB.PhieuDanhGiaNCC> getListPhieu()
        {
            _DB.DataClasses1DataContext dc = new _DB.DataClasses1DataContext();
            IQueryable<_DB.PhieuDanhGiaNCC> listPhieu = 
                            from p in dc.PhieuDanhGiaNCCs
                            where p.deleted == false
                            orderby p.NgayTao descending
                            select p;

            if (!Program.NguoiDungHienTai.coQuyen(35))
                listPhieu = listPhieu.Where(x => x.MaNguoiTao == Program.MaNguoiDungHienTai);

            return listPhieu;
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            showAll = false;
            var listPhieuDG = getListPhieu();

            // filter
            var listPhieu = listPhieuDG.Where(x => !dtpNgayTaoMin.Enabled || x.NgayTao.Date >= dtpNgayTaoMin.Value.Date);
            listPhieu = listPhieu.Where(x => !dtpNgayTaoMax.Enabled || x.NgayTao.Date <= dtpNgayTaoMax.Value.Date);
            listPhieu = listPhieu.Where(x => !dtpNgayDuyetMin.Enabled || x.NgayDuyet.Value.Date >= dtpNgayDuyetMin.Value.Date);
            listPhieu = listPhieu.Where(x => !dtpNgayDuyetMax.Enabled || x.NgayDuyet.Value.Date <= dtpNgayDuyetMax.Value.Date);

            listPhieu = listPhieu.Where(x => !cbbNguoiTao.Enabled || x.NguoiTao.Ten.Contains(cbbNguoiTao.Text));
            listPhieu = listPhieu.Where(x => !cbbNguoiDuyet.Enabled || x.NguoiDuyet.Ten.Contains(cbbNguoiDuyet.Text));
            listPhieu = listPhieu.Where(x => !cbbTrangThai.Enabled || x.TrangThaiPhieu.MoTa.Contains(cbbTrangThai.Text));

            if (clbHangHoa.Enabled)
            foreach (Object o in clbHangHoa.CheckedItems)
            {
                String maHangHoa = ((_DB.HangHoa)o).MaHangHoa;
                listPhieu = listPhieu.Where(x => x.HangHoa_NhaCungCaps.Count(y => y.MaHangHoa == maHangHoa) > 0);
            }

            if (clbNhaCungCap.Enabled)
            foreach (Object o in clbNhaCungCap.CheckedItems)
            {
                String maNCC = ((_DB.NhaCungCap)o).MaNhaCungCap;
                listPhieu = listPhieu.Where(x => x.HangHoa_NhaCungCaps.Count(y => y.MaNhaCungCap == maNCC) > 0);
            }

            loadDataGrid(listPhieu);
        }

        private void btnAll_Click(object sender, EventArgs e)
        {
            showAll = true;
            loadDataGrid(getListPhieu());
        }

        private void loadDataGrid(IQueryable<_DB.PhieuDanhGiaNCC> listPhieu)
        {
            _dgvPhieuDG.Rows.Clear();
            int count = 1;
            foreach (_DB.PhieuDanhGiaNCC p in listPhieu)
            {
                DataGridViewRow row = new DataGridViewRow();
                row.CreateCells(_dgvPhieuDG);
                row.Cells[0].Value = count++;
                row.Cells[1].Value = p.MaPhieu;
                row.Cells[2].Value = p.NguoiTao.Ten;
                row.Cells[3].Value = p.NgayTao;
                row.Cells[4].Value = p.HangHoa_NhaCungCaps.Count;
                row.Cells[5].Value = p.TrangThaiPhieu.MoTa;

                if (p.TrangThaiPhieu.TrangThaiPhieu_ID == _DB.TrangThaiPhieu.KHONG_DUYET_ID)
                {
                    row.Cells[5].Value += " (" + p.LyDoKhongDuyet + ")";
                }

                row.Tag = p;

                _dgvPhieuDG.Rows.Add(row);
            }
        }

        public void refreshGrid()
        {
            if (showAll)
                btnAll_Click(null, null);
            else
                btnSearch_Click(null, null);
        }


    }
}
